package com.teach.service.impl;

import com.teach.dto.ArticleSearch;
import com.teach.entity.Article;
import com.teach.mapper.ArticleMapper;
import com.teach.service.ArticleService;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <pre>
 * TODO : 文章业务逻辑实现类
 * 文章
 * ClassName : com.teach.service.impl.ArticleServiceImpl
 * Author : J.L.Zhou
 * Tel : 15111047708
 * E-Mail : 12020042@qq.com
 * Date : 2020-10-31 19:28
 * Version : 1.0
 * </pre>
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

	@Override
	public QueryWrapper<Article> getQueryWrapper(Article article) {
		QueryWrapper<Article> queryWrapper = new QueryWrapper<Article>();
		if(article.getArticleId() != null) {
			queryWrapper.eq("ARTICLE_ID", article.getArticleId());
		}
		if(article.getArticleTypeId() != null) {
			queryWrapper.eq("ARTICLE_TYPE_ID", article.getArticleTypeId());
		}
		if(article.getArticleCode() != null) {
			queryWrapper.eq("ARTICLE_CODE", article.getArticleCode());
		}
		if(article.getArticleTitle() != null) {
			queryWrapper.eq("ARTICLE_TITLE", article.getArticleTitle());
		}
		if(article.getArticleWorker() != null) {
			queryWrapper.eq("ARTICLE_WORKER", article.getArticleWorker());
		}
		if(article.getArticleContent() != null) {
			queryWrapper.eq("ARTICLE_CONTENT", article.getArticleContent());
		}
		if(article.getArticleStatus() != null) {
			queryWrapper.eq("ARTICLE_STATUS", article.getArticleStatus());
		}
		if(article.getArticleImgs() != null) {
			queryWrapper.eq("ARTICLE_IMGS", article.getArticleImgs());
		}
		if(article.getArticleFiles() != null) {
			queryWrapper.eq("ARTICLE_FILES", article.getArticleFiles());
		}
		if(article.getArticleTags() != null) {
			queryWrapper.eq("ARTICLE_TAGS", article.getArticleTags());
		}
		if(article.getArticleTime() != null) {
			queryWrapper.eq("ARTICLE_TIME", article.getArticleTime());
		}
		return queryWrapper;
	}

	@Override
	public Page<Article> page(ArticleSearch search, Page<Article> page) {
		return baseMapper.selectPage(page, getQueryWrapper(search));
	}

	@Override
	public QueryWrapper<Article> getQueryWrapper(ArticleSearch search) {
		QueryWrapper<Article> qw = new QueryWrapper<>();
		if(search.getArticleTypeId()!=null) {
			qw.lambda().eq(Article::getArticleTypeId, search.getArticleTypeId());//and article_type_id={search.getArticleTypeId()}
		}
		if(!StrUtil.hasBlank(search.getKey())) {
			qw.lambda().like(Article::getArticleTitle, search.getKey());//and article_title like '%{search.getKey()}%'
		}
		if(StrUtil.hasBlank(search.getSort())) {
			qw.lambda().orderByDesc(Article::getArticleTime);//order by article_time desc
		}else if("DESC".equalsIgnoreCase(search.getOrder())) {
			qw.orderByDesc(search.getSort());
		}else {
			qw.orderByAsc(search.getSort());
		}
		return qw;
	}
	

}
